package com.swx.generate;

import com.aspose.words.License;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

@Slf4j
@SpringBootApplication
public class App {
    @Getter
    private String appName;
    private int port;

    @Value("${spring.profiles.active:dev}")
    private String active;

    @Value("${spring.application.name:}")
    public void setAppName(String appName) {
        if (StringUtils.isEmpty(appName)) {
            this.appName = "未命名项目";
        } else {
            this.appName = new String(appName.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        }
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(App.class);
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.run(args);
    }

    @EventListener({WebServerInitializedEvent.class})
    public void afterRun(WebServerInitializedEvent event) throws Exception {

        String host = getHostIp();
        port = event.getWebServer().getPort();

        log.info("\n----------------------------------------------------------\n\t" +
                        "程序【{}】已经运行!\n\t" +
                        "环境：\t{}\n\t" +
                        "主页: \thttp://{}:{}\n\t" +
                        "审计: \thttp://{}:{}/actuator\n\t" +
                        //                        "日志: \thttp://{}:{}/log.html\n\t" +
                        "文档: \thttp://{}:{}/doc.html\n" +
                        "----------------------------------------------------------",
                appName, active, host, port, host, port, host, port
                //                , host, port
        );

        log.info("注册Aspose.words授权码");
        ClassPathResource classPathResource = new ClassPathResource("Aspose.Words.lic");
        try (InputStream inputStream = classPathResource.getInputStream()) {
            License aposeLic = new License();
            aposeLic.setLicense(inputStream);
            log.info("注册Aspose.words授权码完成");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("注册Aspose.words授权码失败", e);
        }
    }

    public String getHostIp() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                if (netInterface.isVirtual() || !netInterface.isUp() || netInterface.isLoopback()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = addresses.nextElement();
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "127.0.0.1";
    }
}
